import pandas as pd
import random
import matplotlib.pyplot as plt
import numpy as np 
from filter import Filter
from datetime import datetime
# 获取当前本地时间（日期+时间）
current_time = datetime.now()
# 格式化为易读的字符串（默认格式：%Y-%m-%d %H:%M:%S）
formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
print("当前本地时间:", formatted_time)
# 1.1 读取两个数据表中的数据
df = pd.read_excel("D:/lxh/DataAna/pac_add_analyse/python/train.xlsx")
train_numpydata = df.to_numpy()
enpty_array = np.zeros((train_numpydata.shape[0], 2)) 
train_numpydata = np.hstack((train_numpydata, enpty_array))
#1.2 提取流动电位仪数据，并统一采样率
df2 = pd.read_excel("D:/lxh/DataAna/pac_add_analyse/python/PotentialData.xlsx")
numpy_array_PotentialData = df2.to_numpy()
# 1.3 读取药水比数据，和训练数据中的投加流量结合，计算出当前pac投加量
df3 = pd.read_excel("D:/lxh/DataAna/pac_add_analyse/python/pac_add_ratio.xlsx")
# 1.3.1 数据中时间点为 0 7 9 13 17 19 21 时间点，扩展数据为10分钟的分辨率，并计算药的投加量
numpy_array_pac_add_ratio = df3.to_numpy()

# ############################################
df['时间'] = pd.to_datetime(df['时间'])
start_row = 0
pac_add_Num = np.array([]) #用于存储所有计算之后的pac投加量数据

for i in range(1,len(numpy_array_pac_add_ratio)):
    start_time = df3['时间'].iloc[i]
    end_time = df3['时间'].iloc[i+1]
    filtered_df = df[(df['时间'] >= start_time) & (df['时间'] < end_time)]
    row_count = filtered_df.shape[0] # 计算start_time和end_time之间有多少行数据
    now_pac_add_rato = numpy_array_pac_add_ratio[i,1:3] # 取出当前时间点的药水比
    
    # 取出numpy_array_pac_add_ratio中的第i行，第二第三列数据
    # 创建一个值为numpy_array_pac_add_ratio[i,1:3]，行数为row_count的数组
    pac_add_ratio_temp = np.tile(numpy_array_pac_add_ratio[i,1:3], (row_count, 1)) 
    # 取出训练数据中的第4列
    pac_add_ratio_Num = df.iloc[start_row:row_count, 3]
    # print(pac_add_ratio_temp.shape, pac_add_ratio_Num.shape)
    pac_add_ratio_Num = pac_add_ratio_Num * pac_add_ratio_temp[:,0]  / pac_add_ratio_temp[:,1] #pac投加量：药水流量*药/水
    
    print(pac_add_Num.shape, pac_add_ratio_Num.shape)

    pac_add_Num = np.vstack((pac_add_Num, pac_add_ratio_Num))

    start_row = row_count






print(start_time, end_time)

# 提取df['时间']数据中，start_time和end_time的行数

filtered_df = df[(df['时间'] >= start_time) & (df['时间'] < end_time)]
row_count = filtered_df.shape[0]
print(row_count)

##############################################


"""
# train_numpydata
# print(numpy_array[0,0],numpy_array[0,1], numpy_array[0,2]) 第一行 时间 药 水
# print("\n")
# print(numpy_array[1,0],numpy_array[1,1], numpy_array[1,2]) 第二行 时间 药 水
# print(train_numpydata[0,0])
# print(numpy_array_pac_add_ratio[0,0])
"""
# 提取 train_numpydata（自动解析时间格式）
datetime_index_train = pd.to_datetime(train_numpydata[:,0])
years_train = datetime_index_train.year
months_train = datetime_index_train.month
days_train = datetime_index_train.day
hours_train = datetime_index_train.hour
minutes_train = datetime_index_train.minute
seconds_train = datetime_index_train.second  
# 提取 numpy_array_pac_add_ratio（自动解析时间格式）
datetime_index_pac_add_ratio = pd.to_datetime(numpy_array_pac_add_ratio[:,0])
years_pac_add_ratio = datetime_index_pac_add_ratio.year
months_pac_add_ratio = datetime_index_pac_add_ratio.month
days_pac_add_ratio = datetime_index_pac_add_ratio.day
hours_pac_add_ratio = datetime_index_pac_add_ratio.hour
minutes_pac_add_ratio = datetime_index_pac_add_ratio.minute
seconds_pac_add_ratio = datetime_index_pac_add_ratio.second  

# 2.1 清洗数据：滤波、补全数据（线性插值）
# 调用filter中的算法滤波
# print(train_numpydata[:,1].shape)
# 中值滤波调用
org_Tur_filterData = Filter(train_numpydata[:,1]).median_filter(train_numpydata[:,1],kernel_size=3)
Out_Tur_filterData = Filter(train_numpydata[:,2]).median_filter(train_numpydata[:,2],kernel_size=3)
PAC_Flow_filterData = Filter(train_numpydata[:,3]).median_filter(train_numpydata[:,3],kernel_size=3)
org_Flow_filterData = Filter(train_numpydata[:,4]).median_filter(train_numpydata[:,4],kernel_size=3)
PotentialData_filterData =  Filter(numpy_array_PotentialData[:,1]).median_filter(numpy_array_PotentialData[:,1],kernel_size=3)
# print(org_Tur_filterData.shape,Out_Tur_filterData.shape,PAC_Flow_filterData.shape,org_Flow_filterData.shape,PotentialData_filterData.shape)
# print(org_Tur_filterData.shape,Out_Tur_filterData.shape,PAC_Flow_filterData.shape,org_Flow_filterData.shape)
# 2.2 对齐各个数据表维度
# 用中值滤波后，五项数据维度相同，暂时不用对齐
# 2.3 计算药的投加量
# train_numpydata 训练数据
# numpy_array_pac_add_ratio 药水比数据
PAC_Add = np.array([len(train_numpydata),1])
# for i in range(len(numpy_array_pac_add_ratio)):
#     for j in range(len(train_numpydata)):
#         numpy_array_pac_add_ratio[]

# 3. 选择合适的机器学习模型训练数据

# 4. 训练模型并保存模型

# 5. 读取7.13-7.22数据做预测并与结果做对比




